﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SmallJob.Services.Code
{
    public class StringHanding
    {
        public static String Convert(String input)
        {
            string result = input.ToUpper();
            string convert = "ĂÂÀẰẦÁẮẤẢẲẨÃẴẪẠẶẬỄẼỂẺÉÊÈỀẾẸỆÔÒỒƠỜÓỐỚỎỔỞÕỖỠỌỘỢƯÚÙỨỪỦỬŨỮỤỰÌÍỈĨỊỲÝỶỸỴĐăâàằầáắấảẳẩãẵẫạặậễẽểẻéêèềếẹệôòồơờóốớỏổởõỗỡọộợưúùứừủửũữụựìíỉĩịỳýỷỹỵđ";
            string To = "AAAAAAAAAAAAAAAAAEEEEEEEEEEEOOOOOOOOOOOOOOOOOUUUUUUUUUUUIIIIIYYYYYDaaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyyd";
            for (int i = 0; i < To.Length; i++)
            {
                result = result.Replace(convert[i], To[i]);
            }
            return result;
        }
        public static bool HasSpecialCharacters(string s)
        {
            string special = " `~!@#$%^&*()-+={}[];:\'\"<,>?/\\|";
            for (int i = 0; i < special.Length; i++)
            {
                if (s.Contains(special[i]))
                    return true;
            }
            return false;
        }
        public static int CountContain(String text1, String text2)
        {
            int result = 0;
            string s1 = StringHanding.Convert(text1);
            string s2 = StringHanding.Convert(text2);
            char[] separator = { ' ' };
            string[] array1 = s1.Split(separator).Where(s => s != "").ToArray();
            foreach (var item in array1)
            {
                if (s2.Contains(item))
                    result++;
            }
            return result;
        }
        public static string HideEmail(string input, string replace, out bool check)
        {
            string result = "";
            string pattern = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
            Regex regex = new Regex(pattern);
            check = regex.IsMatch(input);
            result = regex.Replace(input, "<a href=\"/vip\">" + replace + "</a>"); ;
            return result;
        }
        public static string HidePhone(string input, string replace)
        {
            string result = "";
            string pattern = @"[0-9\-\+\. ]{9,15}";
            Regex regex = new Regex(pattern);
            result = regex.Replace(input, "<a href=\"/vip\">" + replace + "</a>");
            return result;
        }
        public static string Hide(string input, string replacePhone, string replaceEmail, string keywords)
        {
            string result = "";
            bool check = false;
            result = HidePhone(input, replacePhone);
            result = HideEmail(result, replaceEmail, out check);
            if (check)
            {
                foreach (var item in keywords.Split(';'))
                {
                    if (item.Length > 0)
                    {
                        result = Regex.Replace(result, item, "", RegexOptions.IgnoreCase);
                    }
                }
            }
            return result;
        }
    }
}
